<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 用户类  
 */
class AdminUser extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->library('request');  //自定义
        $this->load->library('response'); //自定义
        $this->load->library('session'); //公共
        $reqOrigin = isset($_SERVER['HTTP_ORIGIN'])?$_SERVER['HTTP_ORIGIN']:'';
        $configOrigin = $this->config->item("allowOrigin");
        if(in_array($reqOrigin,$configOrigin)){
            header("Access-Control-Allow-Credentials:true");
            header("Access-Control-Allow-Headers:Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild");
            header("Access-Control-Allow-Methods:PUT, POST, GET, DELETE, OPTIONS");
            header("Access-Control-Allow-Origin:$reqOrigin"); 
        }
    }
    /**
     * [学生首页 description]
     * @return [type] [description]
     */
    public function studentIndex(){

    }

    /**
     * [loginStatus description]
     * @return [type] [description]
     */
    private function loginStatus(){
        if(isset($this->session->admin_user_id) && $this->session->admin_user_id){
            return array("admin_user_id"=>$this->session->admin_user_id,"status"=>0);
        }
        else{
            return array("status"=>2006);
        }

    }

    /**
     * [login description
     * 账号，密码登陆]
     * @return [type] [description]
     */
    public function login(){
        if($this->input->method()=="post"){       
            $data = $this->request->getPostRawData();
            log_message('info', 'adminuser/login,data:'.json_encode($data));     
            if(!$data['account'] || !$data['password'] ){
                $this->response->sendData(1000);return;
            }
            //登陆操作
            $this->load->model("userModel");
            $status = $this->userModel->adminLogin($data['account'],$data['password']);
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);
    }

    /**
     * [studentIdentify description]
     * @return [type] [description]
     */
    public function studentIdentify(){
        if($this->input->method()=="get"){       
            //1登陆状态
            $login = $this->loginStatus();
            if($login['status']){
                $this->response->sendData($login['status']);return;
            }
            $admin_user_id = $login['admin_user_id'];
            // echo $admin_user_id;exit;
            //2获取数据
            $data = $this->request->getGetData(["page","size",'schoolStatus','nick','account']);
            log_message('info', 'adminuser/studentIdentify,data:'.json_encode($data));     
            if(!$data['page'] || !$data['size'] ){
                $this->response->sendData(1000);return;
            }
            //3查询
            $this->load->model("userModel");
            $ret = $this->userModel->adminUserList($data);          
            $this->response->sendData($ret['status'],$ret['data'],$ret['out_data']);return;
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);

    }

    /**
     * [studentUpdate 审核一个]
     * @return [type] [description]
     */
    public function studentUpdate(){
        if($this->input->method()=="post"){       
            $data = $this->request->getPostRawData();
            log_message('info', 'adminuser/studentUpdate,data:'.json_encode($data));     
            if(!$data['id'] || !$data['status'] ){
                $this->response->sendData(1000);return;
            }

            
            //审核
            $this->load->model("userModel");
            $ret = $this->userModel->schoolIdentify($data['id'],$data['status']);
            $this->response->sendData($ret['status']);return;
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);
    }

     /**
     * [logOut 退出]
     * @return [type] [description]
     */
    public function logOut(){
        if($this->input->method()=="post"){       
            $data = $this->request->getPostRawData();
            log_message('info', 'adminuser/logOut,data:'.json_encode($data));     
            //登陆操作
            $this->load->model("userModel");
            $status = $this->userModel->adminLogOut();
        }
        else{
            $status = 1001;
        }
        $this->response->sendData($status);
    }

}
